# conding=utf-8
import sys
import pandas as pd
from paddleocr import PaddleOCR

ocr = PaddleOCR(use_angle_cls=True, lang='ch', use_gpu=True,show_log=False,code='utf-8')

def get_value_by_OCR(img_path,is_and=False,*match_str):
    '''
    将图片中的文字识别出来，并返回所有识别的对结果
    :param img_path:识别图片
    :param is_and:用作判断是匹配字段是且，还是或
    :return:
    '''
    # 如果你的机器有 GPU 并且安装了 paddlepaddle-gpu，可以添加 use_gpu=True 参数来启用 GPU 加速
    ret = []
    result = ocr.ocr(img_path, cls=True)  # cls=True 表示启用方向分类器
    ocr_all_datas = result[0]
    print(f"识别到结果：{ocr_all_datas}")
    if match_str:
        for i in range(len(ocr_all_datas)):
            wenzi = ocr_all_datas[i][1][0]
            if is_and:
                fagle = []
                for agr in match_str:
                    if agr in wenzi:    # 都要满足
                        fagle.append(True)
                    else:
                        fagle.append(False)
                if all(fagle):
                    ret.append(ocr_all_datas[i])
            else:
                for agr in match_str:
                    if agr in wenzi:    # 满足其一即可
                        ret.append(ocr_all_datas[i])
                        break
        return ret
    else:
        return ocr_all_datas

def answer_keju(question_img,answer_img,xls_path):
    '''
    :param question_img: 含问题的图片
    :param answer_img: 含答案的图片
    :return:
    '''
    # 1.截图,区域识别，获取问题
    ret_question = get_value_by_OCR(question_img)  # 根据题目位置，调节识别区域
    print(ret_question)
    if ret_question:
        question_title = ""
        for i in range(len(ret_question)):
            question_title = question_title+ ret_question[i][1][0]
        print(f"该题是：{question_title}")
        # 2.处理数据获取坐标
        ret_data = []
        df = pd.read_excel(xls_path)
        excel_all_data = df.values
        index = 0  # 用于查找excel中的几行
        
        for j in range(len(excel_all_data)):
            keyword = str(excel_all_data[j][1])
            if "，" in keyword:
                keywords = str(keyword).split("，")
            else:
                keywords = [keyword]
            flage = []
            for kw in keywords:
                if kw in question_title:
                    flage.append(True)
                else:
                    flage.append(False)
            if all(flage) and flage:
                ret_data.append(question_title)
                index = j
        #print(f"根据【关键字：{keywords}】获取的文字信息：{ret_data}")
        # 3 从表格中获取答案
        answer = excel_all_data[index][2]
     
      
        if answer:
            # 5 作答
            ret_answer = get_value_by_OCR(answer_img,False,answer)  # 根据题目位置，调节识别区域
            for i in range(len(ret_answer)):
                print(f"识别答案是：{ret_answer[i][1][0]}")
            if ret_answer:
                print(f"该题答案有：{answer}")
                center_x = int((ret_answer[0][0][1][0] + ret_answer[0][0][0][0])/2)
                center_y = int((ret_answer[0][0][3][1] + ret_answer[0][0][1][1])/2)
                print(f"坐标|{center_x}|{center_y}")
                return center_x,center_y
            else:
                print(f"盲猜答题：B")
                ret_answer = get_value_by_OCR(answer_img,False,"B")  # 根据题目位置，调节识别区域
                if ret_answer:
                    center_x = int((ret_answer[0][0][1][0] + ret_answer[0][0][0][0])/2)
                    center_y = int((ret_answer[0][0][3][1] + ret_answer[0][0][1][1])/2)
                    print(f"坐标|{center_x}|{center_y}")
                    return center_x,center_y
        else:
            print("未找到答案！！！！！")
    else:
        print("未找到题目！！！！！")


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print("Usage: python script_name.py <image_path> <image_path> [<match_str1> <match_str2> ...]")
        sys.exit(1)

    img_path = sys.argv[1]
    answer_path = sys.argv[2]
    xml_path = sys.argv[3]
    answer_keju(img_path,answer_path,xml_path)


